<?php
class FtpController extends My_Controller_Base 
{
	public function init() {
		$this->view->baseUrl = $this->_request->getBaseUrl();
		Zend_Registry::set('baseUrl', $this->_request->getBaseUrl());
		$oSesja = new Zend_Session_Namespace('zalogowanyUzytkownik');
		$this->view->user = $oSesja->oUzytkownik;
	}
	
    public function indexAction() {
    }
    
    public function reviseAction() {
    	$aDrzewo = array();
    	Zasob::zrobDrzewo($aDrzewo, 0);
    	$this->view->drzewo = $aDrzewo;
    }
    
    public function deleteAction() {
  		if($iId = $this->getRequest()->getParam('id')) {
 			$oZasob = new Zasob($iId);
			$oZasob->usun();
     	}
     	
     	$this->_redirect('ftp/revise');
    }
    
    public function getAction() {
  		if($iId = $this->getRequest()->getParam('id')) {
 			$oZasob = new Zasob($iId);
			$oZasob->pobierz();
     	}
    }
    
    public function adddirAction() {
	    if($iId = $this->getRequest()->getParam('id')) {
			$oForm = new AddDirForm($iId);
			$this->view->form = $oForm; 
		} elseif($this->getRequest()->isPost()) {
			$oForm = new AddDirForm();
			if($oForm->isValid($_POST)) {
				$aValues = $oForm->getValues();
				Zasob::dodajKatalog(array('id_rodzica' => $aValues['id_rodzica'], 'nazwa' => $aValues['nazwa']));
				$this->_redirect('ftp/revise');
			} else {
				$this->view->form = $oForm;
			}
		} else
			$this->_redirect('ftp/revise');    		
    }
    
    public function addfileAction() {
		if($iId = $this->getRequest()->getParam('id')) {
			$oForm = new AddFileForm($iId);
			$this->view->form = $oForm; 
		} elseif($this->getRequest()->isPost()) {
			$oForm = new AddFileForm();
			if($oForm->isValid($_POST)) {
				$aValues = $oForm->getValues();
				$sFilePath = substr($_FILES['plik']['tmp_name'], 0, strrpos($_FILES['plik']['tmp_name'], "\\"));
				Zasob::dodajPlik(array('id_rodzica' => $aValues['id_rodzica'], 'nazwa' => $_FILES['plik']['name']), $sFilePath, $_FILES['plik']['name']);
				$this->_redirect('ftp/revise');
			} else {
				$this->view->form = $oForm;
			}
		} else
			$this->_redirect('ftp/revise');
	}
    
    public function editAction() {
    	if($this->getRequest()->isGet()) {
			$iId = $this->getRequest()->getParam('id', null);
		
			if($iId != null) {
				$oForm = new EditResourceForm($iId);
				$this->view->form = $oForm;
			} else {
				$this->_redirect('ftp/revise');
			}
		} elseif($this->getRequest()->isPost()) {
			$oForm = new EditResourceForm();
			
			if($oForm->isValid($_POST)) {
				$aValues = $oForm->getValues();
						
				$oZasob = new Zasob($aValues['id_zasobu']);
				$oZasob->edytuj($aValues['nazwa']);
				$this->_redirect('ftp/revise');
			} else {
				$this->view->form = $oForm;
			}
		}
	}
}
?>